package level_ajax_query.dao;

import level_ajax_query.db.JDBCUtil;
import level_ajax_query.entry.City;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Rookie Coder
 * @Description
 * @Date
 **/
public class CityDao {

    /*
     * @Author liu-miss
     * @Description 根据省份id查找对应的城市
     * @Date 8:54 2021/5/12
     * @Param [id]
     * @return java.util.List<City>
     **/
    public List<City> getCity(int id){
        JDBCUtil js=new JDBCUtil();
        List<City> cityList=null;
        Connection coon=null;
        PreparedStatement pst=null;
        ResultSet rs=null;

        coon=js.getConnection();
        String sql="select id,name from city where provinceid=?";
        try {
            pst= coon.prepareStatement(sql);
            pst.setInt(1,id);
            rs= pst.executeQuery();
            if (rs!=null){
                cityList=new ArrayList<>();
             while (rs.next()){
                 City city=new City();
                 city.setId(rs.getInt("id"));
                 city.setName(rs.getString("name"));
                 cityList.add(city);
             }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            js.closeAll(coon,pst,rs);
        }
        return cityList;
    }


}
